// pages/channel/teamManage/teamManage.js
var startPoint;
var app = getApp();
var _PAGESIZE = 10;//默认页量
var _PAGE = 1;//默认页码
var load= true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttonTop: 0,
    buttonLeft: 0,
    windowHeight: '',
    windowWidth: '',
    dataList: [],
    hasMore: false, //数据是否加载完成
    loading: false,
    load_show: false,//整体加一个开关 加载
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this;
    wx.getSystemInfo({
      success: function (res) {
        // 屏幕宽度、高度
        that.setData({
          windowHeight:  res.windowHeight,
          windowWidth:  res.windowWidth,
          buttonTop: res.windowHeight - 100,
          buttonLeft: res.windowWidth - 60
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    _PAGE = 1;
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }
    that.setData({
      loading: true
    })
    _PAGE++;
    that.getList();
  },

  // 获取列表
  getList() {
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    app.ajax({
      url: 'User/Channel/getPromoterList',
      method: "POST",
      load: load,
      data: {
        user_id: user_id,
        page: _PAGE,
        pageSize: _PAGESIZE
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              dataList: res.data.data.data,
              empty: false
            });
          } else {
            that.setData({
              dataList: that.data.dataList.concat(res.data.data.data),
              empty: false
            });
          }
          var hasMore = true;
          if (res.data.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load= false;
        }  else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          load_show: true
        })
      }
    })
  },
  // 团员管理
  goMemberManage(e) {
    wx.navigateTo({
      url: '/pages/channel/memberManage/memberManage?user_id='+e.currentTarget.dataset.id,
    })
  },
  // 分成管理
  goDivideManage(e) {
    var user_id = e.currentTarget.dataset.uid;
    var channel_promoter_id = e.currentTarget.dataset.cid;
    wx.navigateTo({
      url: '/pages/channel/divideManage/divideManage?user_id='+user_id+'&channel_promoter_id='+channel_promoter_id,
    })
  },
  // 删除团员
  deleteMember(e) {
    var that = this;
    var channel_promoter_id = e.currentTarget.dataset.id;
    app.showModal("", "确定删除", function() {
      app.ajax({
        url: 'User/Channel/delPromoter',
        data: {
          channel_promoter_id: channel_promoter_id,
        },
        success: function (res) {
          if (res.data.code == 1000) {
            app.showToast('删除成功', "none", 2000, function () { });
            var tempList = that.data.dataList;
            tempList.forEach((item,index)=>{
              if(item.channel_promoter_id == channel_promoter_id){
                tempList.splice(index, 1);
                that.setData({
                  dataList: tempList
                })
              }
            })
          }
        }
      })
    });
  },
  // 编辑团员
  editMember(e) {
    wx.navigateTo({
      url: '/pages/channel/editMember/editMember?channel_promoter_id='+e.currentTarget.dataset.id,
    })
  },
  // 添加团员
  addMember() {
    wx.navigateTo({
      url: '/pages/channel/addMember/addMember',
    })
  },

  // 拖拽开始
  touchStart(e) {
    startPoint = e.touches[0];
  },
  // 拖拽移动
  touchMove(e) {
    var endPoint = e.touches[e.touches.length - 1];
    var translateX = endPoint.clientX - startPoint.clientX;
    var translateY = endPoint.clientY - startPoint.clientY;
    startPoint = endPoint;
    var buttonTop = this.data.buttonTop + translateY;
    var buttonLeft = this.data.buttonLeft + translateX;
    //判断是移动否超出屏幕
    if (buttonLeft+50 >= this.data.windowWidth){
      buttonLeft = this.data.windowWidth-50;
    }
    if (buttonLeft<=0){
      buttonLeft=0;
    }
    if (buttonTop<=0){
      buttonTop=0;
    }
    if (buttonTop + 50 >= this.data.windowHeight){
      buttonTop = this.data.windowHeight-50;
    }
    this.setData({
      buttonTop: buttonTop,
      buttonLeft: buttonLeft
    })
  },
  // 拖拽移动结束
  touchEnd(e) {

  },
  
})